*-------------------------------------------------------------------------------------------------------*
*RESEARCHERS:		Mette Foged, Linea Hasager and Vasil Yasenov
*PROJECT:			Meta-analysis
*DESCRIPTION:		1  Load database
*					2  Generate additional variables
*					3  Test for publication bias
*-------------------------------------------------------------------------------------------------------*

clear all
set more off

global ctr			nordic resteu northamerica restworld
global x_ctr		eu restworld
global x_macro	 	Unemployment GDPgrowth
global x_study 		i.method i.data_frequency i.skill

use "${data}/database_clean.dta", replace

*-------------------------------------------------------------------------------------------------------*
*-------------------------------------------------------------------------------------------------------*

* Rest of the world
gen restworld=0
replace restworld=1 if eu==0 & northamerica==0

*Nordic countries
gen nordic=0
replace nordic=1 if country == "Norway" | country == "Sweden" |country == "Denmark" |country == "Finland"
	
*Rest of Europe
gen resteu=0
replace resteu=1  if country=="Austria" /*| country=="Finland"*/ | country=="France" | country=="Germany" | country=="Greece" ///
| country=="Ireland" | country=="Italy" | country=="Netherlands" | country=="Portugal" | country=="Spain" ///
| country=="Switzerland" | country=="United Kingdom" | country=="Western Europe" | country=="European Economic Area" | country=="Albania"

tab region*
replace region="Rest of Europe" if resteu==1
replace region="Nordic Countries" if nordic==1
replace region="Rest of World" if region=="Rest of the world"


replace wage = 0 if wage == .
tab wage empl, m

replace skill="All" if skill=="all"
replace skill="High Skill" if skill=="High"
replace skill="Low Skill" if skill=="Low"

replace data_frequency="Short Run" 	if data_frequency=="Annual or more frequent"
replace data_frequency="Long Run" 	if data_frequency=="Less frequent"

gen yearfe = int(firstyear/10) * 10
gen pubyearfe = int(year/10)*10


gen top100="Not top 100"
replace top100="Top 100" if journal_ranking < 101

gen top50="Outside Top 50"
replace top50="Top 50" if journal_ranking < 51

replace method="Simulation" 					if method=="simulation"
replace method="Difference-in-Differences"		if method=="DID"
replace method="Difference-in-Differences IV"	if method=="DIDIV"
replace method="First-Difference"				if method=="FD"
replace method="First-Difference IV"			if method=="FDIV"

gen 	method2="IV" if inlist(method,"IV","First-Difference IV","Difference-in-Differences IV")
replace	method2="OLS" if method=="OLS"
replace	method2="Diff-in-Diff, First-Difference" if inlist(method,"Difference-in-Differences","First-Difference")


gen avg=1 if variation=="Area"
replace avg=0 if variation=="Skill" | variation=="Mixture"

gen refugee="Refugees" if refugees==1
replace refugee="Workers, mixture or other" if refugees==0

summ ratio_f_n, d
gen relativelowskill="Relatively Low Skilled" if ratio_f_n !=.
replace relativelowskill="Relatively High Skilled" if ratio_f_n < 1 & ratio_f_n !=.
replace relativelowskill="Unknown" if ratio_f_n==.



* Weights
bysort paper_id wage: gen p_N = 1/_N
bysort paper_id wage: gen p_N2 = 1/_N if se!=.   

gen p_se=1/se
gen p_score=1/Score
gen p_combi=p_N2*p_se*p_score

gen p_se2=1/(se*se)

gen invse = 1/se

*****************
***************** SCATTER PLOTS
*****************

* WAGE - EVERYTHING
global if if invse <= 80  & wage == 1

qui sum estimate $if, d
local mean = r(p50)

twoway (scatter invse estimate $if, xline(`mean', lcolor(black)) msymbol(circle_hollow) mlcolor(gs5)), ///
	xtitle("Estimate") ///
	ytitle("Precision, 1/ SE") ///
	name(one, replace) ///
	xlabel(-3(1)3) graphregion(color(white))
graph export "${out}/funnel_wage.eps", replace	


* EMPLOYMENT - EVERYTHING
global if if invse <= 80  & wage == 0

qui sum estimate $if, d
local mean = r(p50)
twoway (scatter invse estimate $if, xline(`mean', lcolor(black)) msymbol(circle_hollow) mlcolor(gs5)), ///
	xtitle("Estimate") ///
	ytitle("Precision, 1/ SE") ///
	name(two, replace) ///
	xlabel(-3(1)3) graphregion(color(white))
graph export "${out}/funnel_empl.eps", replace		



*****************/
*****************
*****************
gen cons = 1

label var se "Standard Error"
label var cons "Publication Bias Term"


	
**** COMBINED WAGE AND EMPLOYMENT
	
	*Wage
global if if wage == 1  

eststo clear
eststo: qui reg t cons invse $if, cluster(paper_id) nocons
eststo: qui reg t cons invse $if [pw=p_N], cluster(paper_id) nocons
eststo: qui reg t cons invse $if [pw=p_score], cluster(paper_id) nocons
eststo: qui reg t cons invse $if [pw=p_se], cluster(paper_id) nocons
eststo: qui reg t cons invse $if [pw=p_combi], cluster(paper_id) nocons
esttab, keep(cons) b(3) se(3) star(* .1 ** .05 *** .01) 

esttab using "${out}/pbias_combined.tex", replace label ///
	keep(cons) b(%4.3f) se(%4.3f) ///
	nolines mtitles("Unweighted" "Studies" "Impact" "Precision" "Combined") prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{tabular}{l*{5}{c}}\toprule") posthead("\hline & \multicolumn{5}{c}{\emph{Panel A. Wage}} \\") postfoot("") ///
	star(* 0.1 ** 0.05 *** 0.01) nonotes ///
	stats(N r2, label("N" "R$^2$") fmt(%4.0f %4.3f))	
	
	*Employment
global if if wage == 0 
	
eststo clear
eststo: qui reg t cons invse $if, cluster(paper_id) nocons
eststo: qui reg t cons invse $if [pw=p_N], cluster(paper_id) nocons
eststo: qui reg t cons invse $if [pw=p_score], cluster(paper_id) nocons
eststo: qui reg t cons invse $if [pw=p_se], cluster(paper_id) nocons
eststo: qui reg t cons invse $if [pw=p_combi], cluster(paper_id) nocons
esttab, keep(cons) b(3) se(3) star(* .1 ** .05 *** .01) 

esttab using "${out}/pbias_combined.tex", append label ///
	keep(cons) b(%4.3f) se(%4.3f) ///
	nolines nomtitles nonum posthead(" & & & & & \\ & \multicolumn{5}{c}{\emph{Panel B. Employment}} \\") postfoot("\bottomrule \end{tabular}}") prehead("") ///
	star(* 0.1 ** 0.05 *** 0.01) nonotes ///
	stats(N r2, label("N" "R$^2$") fmt(%4.0f %4.3f))
	
	